
from flair.data import Sentence
from flair.models import SequenceTagger
import torch
import torch_npu
import flair
 
if torch.npu.is_available():
    print("NPU is available.")
    flair.device = torch.device('npu')
else:
    print("NPU is not available.")
    flair.device = torch.device('cpu')
    
# 加载命名实体识别模型和情感分析模型
tagger = SequenceTagger.load("./models/en-ner-conll03-v0.4.pt")
# 检查模型使用的设备
device = next(tagger.parameters()).device
print(f"Model is currently using device: {device}")

# 输入语句
print('Please enter the sentence to be analyzed:')
inputs=input()

# NER
tagger_sentence = Sentence(inputs)
tagger.predict(tagger_sentence)
print(tagger_sentence.labels)
